<?php

namespace App\Http\Controllers\Company;

use App\Facades\Style;
use App\Http\Controllers\Company\CommonController;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Company\CompanyStartStop;
use App\Models\Personal\ResumeSend;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ContractController extends CommonController{

    /**
     * 合同记录
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request){
        $user = auth()->guard('company')->user();
        //获取面试通知记录
        $list = DB::table('company_contract as log')
            ->leftJoin('vip_package as t1', 'log.category', '=', 't1.id')
            ->select(
                't1.vipname', 'log.id', 'log.start_time', 'log.end_time', 'log.vip_resume_number', 'log.vip_integral', 'log.vip_post_number', 'log.created_at'
            )
            ->where('log.uid', $user->uid)
            ->orderBy('log.updated_at', 'DESC')
            ->paginate(20);

        if($user->viptime > date('Y-m-d H:i:s')){//会员优先使用会员积分
            $integral = $user->vip_integral;
            $resume_number = $user->vip_resume_number;
            $post_number = $user->vip_post_number;
            if($integral <= 0){
                $integral = $user->integral;
            }
            if($resume_number <= 0){
                $resume_number = $user->resume_number;
            }
            $user_type = 'vip';
            $vipday = $this->timeDiff($user->viptime)[0];
        }else{
            $integral = $user->integral;
            $resume_number = $user->resume_number;
            $post_number = 0;
            $user_type = 'general';
            $vipday = 0;
        }
        $vip = array(
            'day' => $vipday,
            'integral' => $integral,
            'resume_number' => $resume_number,
            'post_number' => $post_number,
            'user_type' => $user_type
        );
        return Style::view('company.contractIndex', compact('list', 'vip'));
    }

    /**
     * 启停功能
     * @return mixed
     */
    public function startStop(){
        $user = auth()->guard('company')->user();
        $companyStartStop = '';
        if(!empty($user)){
            $companyStartStop = CompanyStartStop::where('company_uid', $user->uid)->take(20)->orderBy('created_at', 'desc')->get();
        }
        return Style::view('company.startStop', compact('companyStartStop'));
    }

    /**
     * 设置启停功能
     * @return mixed
     */
    public function setStartStop(Request $request){
        //开启或者关闭启停
        $s_type = (!empty($request->s_type)) ? $request->s_type : '0';
        $user = auth()->guard('company')->user();
        //启停次数是不是存在
        if(!empty($user) && $s_type == '1'){
            if($user->stop_number > 0 && $user->surplus_day > 0){
                $user->stop_number = (int)$user->stop_number - 1;
                $user->viptime = date('Y-m-d H:i:s', strtotime('+' . $user->surplus_day . ' days'));
                $user->surplus_day = 0;
                $user->save();
                $data = [
                    'company_uid' => $user->uid,
                    'status' => 1,
                    'created_at' => date('Y-m-d H:i:s', time())
                ];
                CompanyStartStop::create($data);
                return response()->json(['status' => 0, 'msg' => '开启会员启停成功！']);
            }else{
                return response()->json(['status' => 1, 'msg' => '对不起，您的次数用完或者会员已到期！']);
            }
        }else{
            if($user->stop_number > 0 && $user->viptime > date('Y-m-d H:i:s', time())){
                $time1 = (int)strtotime($user->viptime) - strtotime(date('Y-m-d H:i:s', time()));
                $surplus_day = ceil($time1 / (24 * 60 * 60));
                $user->surplus_day = $surplus_day;
                $user->viptime = date('Y-m-d H:i:s', time());
                $user->save();
                $data = [
                    'company_uid' => $user->uid,
                    'status' => 0,
                    'created_at' => date('Y-m-d H:i:s', time())
                ];
                CompanyStartStop::create($data);
                return response()->json(['status' => 0, 'msg' => '关闭会员启停成功！']);
            }else{
                return response()->json(['status' => 1, 'msg' => '对不起，您的次数用完或者会员已到期！']);
            }
        }
    }


    /**
     * 功能：计算两个日期相差 年 月 日
     * @param date $endtime 截止日期日期
     * @return array(int,'单位')
     */
    protected function timeDiff($endtime){
        $starttime = time();
        $endtime = strtotime($endtime);

        $timediff = $endtime - $starttime;
        $days = intval($timediff / 86400);
        $remain = $timediff % 86400;
        $hours = intval($remain / 3600);
        $remain = $remain % 3600;
        $mins = intval($remain / 60);
        $secs = $remain % 60;
        if($days > 0) return array($days, '天');
        if($hours > 0) return array($hours, '小时');
        if($mins > 0) return array($mins, '分种');
        return array($secs, '秒');
    }
}